package com.demo.jianjunhuang.webviewdemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.demo.jianjunhuang.catimgwebview.CatImgWebView;
import com.demo.jianjunhuang.catimgwebview.CatWebViewClient;
import com.demo.jianjunhuang.catimgwebview.JavascriptInterface;
import com.library.jianjunhuang.okhttputils.okhttputils.OkHttpUtils;
import com.library.jianjunhuang.okhttputils.okhttputils.callback.ResultCallback;

import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity implements DownLoadHTML.HTMLPageDownloaderListener{

    private String url = "http://blog.csdn.net/qq_31876841/article/details/71600954";

    private WebView webView;

    private static final String TAG = "MainActivity";

    private CatWebViewClient catWebViewClient;

    private ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
        progress = (ProgressBar) findViewById(R.id.progress);
        initWebView();
    }


    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        }
    }

    private void initWebView() {

        catWebViewClient = new CatWebViewClient();


        catWebViewClient.setCanInterceptImg(true, "ic_launcher_round.png");
        catWebViewClient.setCanImgClick(true);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.addJavascriptInterface(new JavascriptInterface(this), catWebViewClient.getJSInterfaceName());
//        webView.setWebViewClient(catWebViewClient);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progress.setProgress(newProgress);
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        webView.loadUrl(url);
    }

    @Override
    public void completionCallBack(String html) {
        Log.i(TAG, "completionCallBack: "+html);
//        initWebView(html);
    }

    public class JavascriptInterface {

        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }

        private String imgUrl;

        @android.webkit.JavascriptInterface
        public void onImageUrl(final String imgUrl, final String id) {
            Toast.makeText(context, imgUrl + " " + id, Toast.LENGTH_SHORT).show();
            webView.post(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("javascript:(function(){" +
                            "var img = document.getElementById(\"" + id + "\");" +
                            "if(img != null){" +
                            "img.src = \"http://a2.att.hudong.com/47/77/20300001128119146180778465351_s.gif\";" +
                            "}})()");
                }
            });
            CatWebViewClient.imgUrls.add(url);

        }

        public String getImgUrl() {
            return imgUrl;
        }

        public void setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
        }

        public void showImage(WebView webView, String imgUrl) {
            Log.i(TAG, "showImage: showImage ");
//            String url = "javascript: (function() {
//            webView.loadUrl(url);
//            webView.reload();
        }
    }
}
